package com.lancoo.answer.view.fragment.cloze

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.lancoo.answer.R
import com.lancoo.answer.component.OptionComponent
import com.lancoo.answer.databinding.EvViewClozeBinding
import com.lancoo.answer.model.AnswerConstant
import com.lancoo.answer.model.ConstantBean
import com.lancoo.answer.model.bean.Ques
import com.lancoo.answer.util.paperUtils.FragmentCommunicationUtils
import com.lancoo.answer.view.base.BaseVMFragment
import com.lancoo.answer.view.fragment.option.SingleOptionFragment
import org.greenrobot.eventbus.EventBus

/**
 * Created by Dadong on 2021/12/16
 * Describe: 完形填空作答方式
 * 小题抽题
 */
class ClozeChildFragment : BaseVMFragment<EvViewClozeBinding>() {

    private lateinit var ques: Ques
    private var typeIndex = 0
    private var quesIndex = 0
    private var defaultLoadIndex = 0
    private var chooseCallback: SingleOptionFragment.ChooseCallback? = null
    private var currentPosition: Int = 0
    private var pageChangeCallback: PageChangeCallback? = null
    private var titleVisible = View.VISIBLE

    companion object {
        fun loadFragment(typeIndex: Int, quesIndex: Int, defaultLoadIndex: Int): Fragment? {
            val mBundle = Bundle()
            mBundle.putInt(AnswerConstant.KEY_TYPEINDEX, typeIndex)
            mBundle.putInt(AnswerConstant.KEY_QUESINDEX, quesIndex)
            mBundle.putInt(AnswerConstant.KEY_LOADCHILDINDEX, defaultLoadIndex)
            val clozeChildFragment = ClozeChildFragment()
            clozeChildFragment.arguments = mBundle
            return clozeChildFragment
        }
    }

    override fun initView(view: View) {
    }

    override fun initData() {
        if (ConstantBean.constantExamBean == null) {
            return
        }
        val kindBeanList = ConstantBean.constantExamBean!!.TypeList
        typeIndex = requireArguments().getInt(AnswerConstant.KEY_TYPEINDEX)
        quesIndex = requireArguments().getInt(AnswerConstant.KEY_QUESINDEX)
        defaultLoadIndex = requireArguments().getInt(AnswerConstant.KEY_LOADCHILDINDEX)
        ques = kindBeanList!![typeIndex].QuesList!![quesIndex]
        mBinding.childStatisticsView.setQues(ques)
        initVp()
    }

    private fun initVp() {
        mBinding.childStatisticsView.visibility = titleVisible
        mBinding.vpChildAsk.adapter =
            object : FragmentStateAdapter(childFragmentManager, lifecycle) {
                override fun getItemCount(): Int {
                    return ques.ChildList!!.size
                }

                override fun createFragment(position: Int): Fragment {
                    val singleOptionFragment =
                        SingleOptionFragment.loadFragment(
                            typeIndex,
                            quesIndex,
                            defaultLoadIndex
                        ) as SingleOptionFragment
                    if (ques.GenreID == "65" || ques.GenreID == "62" || ques.GenreID == "61" || ques.GenreID == "79") {
                        singleOptionFragment.setTvChildVisibility(View.GONE)
                    }
                    singleOptionFragment.setChildIndex(position)
                    if (chooseCallback != null) {
                        singleOptionFragment.setChooseCallback(chooseCallback!!)
                    }

                    return singleOptionFragment
                }
            }

        mBinding.vpChildAsk.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                mBinding.childStatisticsView.updateChildIndex(position)
                EventBus.getDefault().post(position)
                currentPosition = position
                pageChangeCallback?.callback(position)
                FragmentCommunicationUtils.switchTestQuesionToEvent(
                    typeIndex,
                    quesIndex,
                    currentPosition,
                    false,
                    1
                )
            }
        })

        if (defaultLoadIndex != -1 && defaultLoadIndex < ques.ChildList!!.size) {
            mBinding.vpChildAsk.postDelayed(Runnable {
                mBinding.vpChildAsk.currentItem = defaultLoadIndex
            }, 200)
        }
    }

    fun setChooseCallback(chooseCallback: SingleOptionFragment.ChooseCallback) {
        this.chooseCallback = chooseCallback
    }

    //换页
    fun nextChildAsk(position: Int? = -1) {
        // 切换到下一页
        if (position == null || position == -1) {
            getCurrentPosition()
            mBinding.vpChildAsk.setCurrentItem(currentPosition, true)
        } else {
            mBinding.vpChildAsk.setCurrentItem(position, true)
        }

    }

    private fun getCurrentPosition() {
        currentPosition += 1
        if (currentPosition >= ques.ChildList!!.size) {
            currentPosition = ques.ChildList!!.size - 1
            FragmentCommunicationUtils.autoNextQuesFragment(this, true)
            return
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.ev_view_cloze
    }

    fun setPageChangeCallback(pageChangeCallback: PageChangeCallback) {
        this.pageChangeCallback = pageChangeCallback
    }

    fun titleVisible(visible: Int) {
        titleVisible = visible
    }

    interface PageChangeCallback {
        fun callback(position: Int)
    }

    fun setCurrentItem(itemIndex: Int) {
        mBinding.vpChildAsk.currentItem = itemIndex
    }

    fun adjustFontSize() {
        if ( mBinding.vpChildAsk.getChildAt(currentPosition)!=null)
            mBinding.vpChildAsk.getChildAt(currentPosition).findViewById<OptionComponent>(R.id.option_component).adjustFontSize()
    }

}